package com.xiaoyi.ui.view;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.avin.yi.R;
import com.xiaoyi.adapter.ListTxtItemAdapter;
import com.xiaoyi.listener.OnMusicItemClickListener;
import com.xiaoyi.tool.MyDeviceInforTool;

import java.util.List;

public class TxtItemListDialog extends Dialog {

    private RecyclerView recyclerView;
    private ListTxtItemAdapter historyAdapter;
    private List<String> datas;
    private OnMusicItemClickListener onItemClickListener;
    private Context activity;


    public TxtItemListDialog(Context context   , List<String> list ) {
        super(context ,R.style.StyleDialog);
        this.activity = context;
        datas = list ;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_musiclist_layout);

        recyclerView =findViewById(R.id.recyclerview) ;

        Window dialogWindow =  getWindow();
        if(dialogWindow!=null){
            dialogWindow.setBackgroundDrawableResource(R.color.color_white);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.TOP );
            dialogWindow.setWindowAnimations(R.style.DialogEnter);
            lp.width =  MyDeviceInforTool.getWindowWidth(activity);
            lp.height =  (int)( MyDeviceInforTool.getWindowHeigh(activity)* (2f/3));

        }

        initAdapter(activity);
    }

    public void setSelectedItemColor( int selectedPosition){
        if(historyAdapter==null){
            historyAdapter = new ListTxtItemAdapter(activity, datas);
        }
        historyAdapter.setSelectedItemColor(selectedPosition);
    }

    private void initAdapter(Context context)   {

        if(historyAdapter==null){
            historyAdapter = new ListTxtItemAdapter(context, datas);
        }

        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        recyclerView.setAdapter(historyAdapter);
        historyAdapter.setOnItemClickListener(new OnMusicItemClickListener() {
            @Override
            public void onItemClick( int position) {
                onItemClickListener.onItemClick(position);
                dismiss();
            }
        });

    }


    public void setDatas(List<String> list){
        this.datas = list ;
        historyAdapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnMusicItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
